/* Copyright (C) 2010 Jerome Arnaud and Voxisland (www.voxisland.com)
 * Copyright (C) 2008 The Android Open Source Project
 * 
 * This file is part of PokerBlinds.
 *
 *  PokerBlinds is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  PokerBlinds is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with PokerBlinds.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.voxisland.pokerblinds;

import java.util.StringTokenizer;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TableLayout.LayoutParams;

public class CustomModel extends Activity {
	public static final String	DEFAULT_CUSTOM_BLINDS			= "100 0 10 200 0 10 400 0 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0";
	public static final int				CUSTOM_BLINDS_NB_ROUNDS	= 30;
	private int[][]												data;
	private Context												thisContext													= this;
	private EditText[]									blinds																		= new EditText[CUSTOM_BLINDS_NB_ROUNDS];
	private EditText[]									antes																			= new EditText[CUSTOM_BLINDS_NB_ROUNDS];
	private EditText[]									durations															= new EditText[CUSTOM_BLINDS_NB_ROUNDS];
	private CheckBox[]									checks																		= new CheckBox[CUSTOM_BLINDS_NB_ROUNDS];
	private static final int			SBLIND																		= 0;
	private static final int			ANTE																				= 1;
	private static final int			DURATION																= 2;

	@Override protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.custom);

		ImageView but_ante_question = (ImageView) findViewById(R.id.ante_question);
		but_ante_question.setOnClickListener(new OnClickListener() {
			@Override public void onClick(View v) {
				AlertDialog.Builder alertbox = new AlertDialog.Builder(CustomModel.this);
				alertbox.setMessage(getText(R.string.ante_def));
				alertbox.setNeutralButton("Ok", null);
				alertbox.show();
			}
		});

		data = new int[CUSTOM_BLINDS_NB_ROUNDS][3];

		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(thisContext);
		String s = sp.getString("custom", DEFAULT_CUSTOM_BLINDS);
		StringTokenizer st = new StringTokenizer(s);

		try {
			for (int round = 0; round < CUSTOM_BLINDS_NB_ROUNDS; round++) {
				data[round][SBLIND] = Integer.valueOf(st.nextToken());
				data[round][ANTE] = Integer.valueOf(st.nextToken());
				data[round][DURATION] = Integer.valueOf(st.nextToken());
			}
		} catch (Exception e) {
			// something went wrong, let's consider the saved value is corrupted or of a
			// previous format
			Log.e("PokerBlinds", "corrupted model?");
			s = DEFAULT_CUSTOM_BLINDS;
			st = new StringTokenizer(s);
			for (int round = 0; round < CUSTOM_BLINDS_NB_ROUNDS; round++) {
				data[round][SBLIND] = Integer.valueOf(st.nextToken());
				data[round][ANTE] = Integer.valueOf(st.nextToken());
				data[round][DURATION] = Integer.valueOf(st.nextToken());
			}
		}

		TableLayout tl = (TableLayout) findViewById(R.id.tab);

		for (int round = 0; round < CUSTOM_BLINDS_NB_ROUNDS; round++) {
			TableRow tr = new TableRow(this);
			tr.setGravity(Gravity.CENTER_HORIZONTAL);
			TextView a = new TextView(this);
			a.setTextColor(Color.WHITE);
			a.setText("#" + Integer.toString(round + 1));
			tr.addView(a);

			EditText b = blinds[round] = new EditText(this);
			b.setText(Integer.toString(data[round][SBLIND]));
			b.setTextColor(Color.BLACK);
			tr.addView(b);

			EditText e = antes[round] = new EditText(this);
			e.setText(Integer.toString(data[round][ANTE]));
			e.setTextColor(Color.BLACK);
			tr.addView(e);

			EditText c = durations[round] = new EditText(this);
			c.setText(Integer.toString(data[round][DURATION]));
			c.setTextColor(Color.BLACK);
			tr.addView(c);

			CheckBox d = checks[round] = new CheckBox(this);
			d.setTag(new Integer(round));
			if (data[round][SBLIND] > 0)
				d.setChecked(true);
			else {
				d.setChecked(false);
				b.setEnabled(false);
				e.setEnabled(false);
				c.setEnabled(false);
			}
			tr.addView(d);

			d.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				@Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if (isChecked) {
						blinds[(Integer) buttonView.getTag()].setEnabled(true);
						antes[(Integer) buttonView.getTag()].setEnabled(true);
						durations[(Integer) buttonView.getTag()].setEnabled(true);
					} else {
						blinds[(Integer) buttonView.getTag()].setEnabled(false);
						durations[(Integer) buttonView.getTag()].setEnabled(false);
						antes[(Integer) buttonView.getTag()].setText("0");
						durations[(Integer) buttonView.getTag()].setText("0");
					}
				}
			});

			tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		}

		Button s1 = (Button) findViewById(R.id.save1);
		Button s2 = (Button) findViewById(R.id.save2);
		OnClickListener ocl = new OnClickListener() {
			@Override public void onClick(View v) {
				String s = "";
				for (int round = 0; round < CUSTOM_BLINDS_NB_ROUNDS; round++) {
					try {
						s = s + Integer.valueOf(blinds[round].getText().toString()) + " " + antes[round].getText().toString() + " " + Integer.valueOf(durations[round].getText().toString()) + " ";
					} catch (Exception e) {
						Toast.makeText(thisContext, R.string.i_ll_save_when_you_ll_have_valid_data_for_me, Toast.LENGTH_LONG).show();
						return;
					}
				}
				SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(thisContext);
				sp.edit().putString("custom", s).commit();
				finish();
			}
		};
		s1.setOnClickListener(ocl);
		s2.setOnClickListener(ocl);
	}
}
